package com.terrier.ui.effects
{
    import com.terrier.ui.events.WxEffectEvent;
    
    import flash.display.DisplayObject;
    import flash.events.EventDispatcher;

    /**
     * 抽象特效类
     *
     * @author Jarvis.weng
     */
    public class WxAbstractEffect extends EventDispatcher implements IWxAbstractEffect
    {
        private var _target:DisplayObject;
        private var _paused:Boolean;
        private var _running:Boolean;
		
		private var _runTime:Number = 0.5;
		
		private var _startEventParams:Object;
		private var _updateEventParams:Object;
		private var _stopEventParams:Object;

        /**
         * 构造函数
         *
         * @param target 应用该特效的对象
         */
        public function WxAbstractEffect(target:DisplayObject, runTime:Number=0.0,
										 startEventParams:Object = null,
										 updateEventParams:Object = null,
										 stopEventParams:Object = null)
        {
            _target = target;
			_runTime = runTime;
			
			_startEventParams = startEventParams;
			_updateEventParams = updateEventParams;
			_stopEventParams = stopEventParams;
        }

        //--------------------------------------------------------
        //
        // Properties
        //
        //--------------------------------------------------------

        /**
         * 应用该特效的对象
         *
         * @return
         */
        public function get target():DisplayObject
        {
            return _target;
        }

        /**
         * @private
         * @param value
         */
        public function set target(value:DisplayObject):void
        {
            _target = value;
        }

        /**
         * 特效 暂停状态; 如果暂停,返回true,否则返回false
         * @return
         */
        public function get paused():Boolean
        {
            return _paused;
        }

        /**
         * @private
         * @param value
         */
        public function set paused(value:Boolean):void
        {
            if (_paused != value)
            {
                _paused = value;

                var event:WxEffectEvent = null;

                if (paused)
                {
                    event = new WxEffectEvent(WxEffectEvent.EFFECT_PAUSE);
                }
                else
                {
                    event = new WxEffectEvent(WxEffectEvent.EFFECT_RESUME);
                }

                dispatchEvent(event);
            }
        }

        /**
         * 特效运行状态;正在运行,返回true,否则返回false
         *
         * @return
         */
        public function get running():Boolean
        {
            return _running;
        }
		
		/**
		 * Animation speed. Units: seconds
		 */
		public function get runTime():Number
		{
			return _runTime;
		}
		
		public function set runTime(value:Number):void
		{
			this._runTime = value;
		}
		
		public function get startEventParams():Object
		{
			return this._startEventParams;
		}
		
		public function set startEventParams(value:Object):void
		{
			this._startEventParams = value;
		}
		
		public function get updateEventParams():Object
		{
			return this._updateEventParams;
		}
		
		public function set updateEventParams(value:Object):void
		{
			this._updateEventParams = value;
		}
		
		public function get stopEventParams():Object
		{
			return this._stopEventParams;
		}
		
		public function set stopEventParams(value:Object):void
		{
			this._stopEventParams = value;
		}

        //--------------------------------------------------------
        //
        // Methods
        //
        //--------------------------------------------------------

        /**
         * 开始特效动画
         */
        public function start():void
        {
            _running = true;
            initialize();

            var event:WxEffectEvent = new WxEffectEvent(WxEffectEvent.EFFECT_START);
			event.eventParams = this._startEventParams;
            dispatchEvent(event);
        }

        /**
         * 停止特效动画
         */
        public function stop():void
        {
            _running = false;
            var event:WxEffectEvent = new WxEffectEvent(WxEffectEvent.EFFECT_STOP);
			event.eventParams = this._stopEventParams;
            dispatchEvent(event);
        }

        /**
         * 初始化特效
         */
        protected function initialize():void
        {
            throw new Error("实例化了抽象类或者没有实现抽象类方法");
        }

        /**
         * 撤销特效
         */
        public function restore():void
        {
            throw new Error("实例化了抽象类或者没有实现抽象类方法");
        }
		
		public function clone():IWxAbstractEffect
		{
			var newEffect:WxAbstractEffect = new WxAbstractEffect(this.target, this.runTime, 
						this._startEventParams,
						this._updateEventParams, 
						this._stopEventParams);
			
			return newEffect;
		}
    }
}